/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "timers.h"
#include "keyled.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
uint32_t counter=0;
/* USER CODE END Variables */
/* Definitions for Task_Main */
osThreadId_t Task_MainHandle;
const osThreadAttr_t Task_Main_attributes = {
  .name = "Task_Main",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for Timer_Periodic */
osTimerId_t Timer_PeriodicHandle;
const osTimerAttr_t Timer_Periodic_attributes = {
  .name = "Timer_Periodic"
};
/* Definitions for Timer_Once */
osTimerId_t Timer_OnceHandle;
const osTimerAttr_t Timer_Once_attributes = {
  .name = "Timer_Once"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void AppTask_Main(void *argument);
void AppTimer_Periodic(void *argument);
void AppTimer_Once(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* Create the timer(s) */
  /* creation of Timer_Periodic */
  Timer_PeriodicHandle = osTimerNew(AppTimer_Periodic, osTimerPeriodic, NULL, &Timer_Periodic_attributes);

  /* creation of Timer_Once */
  Timer_OnceHandle = osTimerNew(AppTimer_Once, osTimerOnce, NULL, &Timer_Once_attributes);

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of Task_Main */
  Task_MainHandle = osThreadNew(AppTask_Main, NULL, &Task_Main_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_AppTask_Main */
/**
  * @brief  Function implementing the Task_Main thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_AppTask_Main */
void AppTask_Main(void *argument)
{
  /* USER CODE BEGIN AppTask_Main */
	xTimerChangePeriod(Timer_OnceHandle,pdMS_TO_TICKS(5000),portMAX_DELAY);
	xTimerChangePeriod(Timer_PeriodicHandle,pdMS_TO_TICKS(1000),portMAX_DELAY);

	LED2_ON();
	counter=0;
	xTimerStart(Timer_OnceHandle,portMAX_DELAY);
	xTimerStart(Timer_PeriodicHandle,portMAX_DELAY);
  /* Infinite loop */
  for(;;)
  {
    KEYS curKey=ScanPressedKey(20);
    if(curKey==KEY0)
    {
    	counter=0;
    	if(xTimerIsTimerActive(Timer_OnceHandle)==pdFALSE)
    		LED2_ON();
    	xTimerReset(Timer_OnceHandle,portMAX_DELAY);
    	vTaskDelay(300);
    }
    else
    	vTaskDelay(10);
  }
  /* USER CODE END AppTask_Main */
}

/* AppTimer_Periodic function */
void AppTimer_Periodic(void *argument)
{
  /* USER CODE BEGIN AppTimer_Periodic */
	LED1_Toggle();
	counter++;
	LCD_ShowUint(50, 200, counter);
  /* USER CODE END AppTimer_Periodic */
}

/* AppTimer_Once function */
void AppTimer_Once(void *argument)
{
  /* USER CODE BEGIN AppTimer_Once */
	LED2_Toggle();
  /* USER CODE END AppTimer_Once */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

